client/verb

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
///  Message Window Control  ///
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

	countIMs()
		set hidden = 1
		var
			windows = winget(src,null,"windows")
			windowlist = list()
			window = ""
			count = 0

		while(length(windows) > 0)
			window = copytext(windows, 1, findtext(windows, ";"))
			windowlist += window
			windows = copytext(windows, length(window)+2)

		for(var/i in windowlist)
			if(findtext(i, "IM_",1,4))
				if(winget(src,i,"is-visible") == "true")
					count++

		IM_Windows = count

		if(IM_Windows > 0) winset(src,"Main","title=\"([IM_Windows]) The Saloon\"")
		else winset(src,"Main","title=\"The Saloon\"")



	Create_IM(var/sender as text, var/target as text)
		set hidden = 1
		if(!findTarget(target)) return
		if(!findTarget(sender)) return
		else
			// Sending person
			var/client/person = findTarget(target)
			var/client/source = findTarget(sender)
			if(source.name in person.Ignored) return
			if(person.name in source.Ignored) return
			if(!person.IM_ENABLE)
				System_UserMessage(src,"That person has Instant Messages disabled.")
				return
			if(!source.IM_ENABLE)
				System_UserMessage(src,"You have Instant Messages disabled.")
				return

			winclone(source, "IM", "IM_[person.ckey]")
			winclone(person, "IM", "IM_[source.ckey]")

			winset(source, "IM_[person.ckey].IM_image1", "image=\"[source.User_Image]\"; image-mode=stretch")
			winset(source, "IM_[person.ckey].IM_image2", "image=\"[person.User_Image]\"; image-mode=stretch")
			winset(source, "IM_[person.ckey].IM_displayname1", "text=\"[source.key]\"")
			winset(source, "IM_[person.ckey].IM_displayname2", "text=\"[person.key]\"")
			winset(source, "IM_[person.ckey]", "title=\"[person.key]\"")

			winset(person, "IM_[source.ckey].IM_image1", "image=\"[person.User_Image]\"; image-mode=stretch")
			winset(person, "IM_[source.ckey].IM_image2", "image=\"[source.User_Image]\"; image-mode=stretch")
			winset(person, "IM_[source.ckey].IM_displayname1", "text=\"[person.key]\"")
			winset(person, "IM_[source.ckey].IM_displayname2", "text=\"[source.key]\"")
			winset(person, "IM_[source.ckey]", "title=\"[source.key]\"")

			winset(source, "IM_[person.ckey]", "is-visible=true")
			winset(source, "IM_[person.ckey].IM_input", "focus=true")

			person.countIMs()
			source.countIMs()

	CloseIM()
		set hidden = 1
		countIMs()

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*
///    Actual Messages       ///
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

	Send_IM(msg as text|null)
		set hidden = 1
		set name = ">>"

		if(msg == null) return

		var/target = copytext(winget(src, null, "focus"),4,findtext(winget(src, null, "focus"),"."))
		if(!findTarget(target)) return

		else
			var/client/person = findTarget(target)
			if(src.key in person.Ignored) return
			if(!src.IM_ENABLE)
				System_UserMessage(src,"You have Instant Messages disabled.")
				return
			if(!person.IM_ENABLE)
				System_UserMessage(src,"That person has Instant Messages disabled.")
				return

			winset(person, "IM_[src.ckey]", "is-visible=true")
			if(validMessageCheck(msg, 1))
				setStatus("Available")
				msg = linkSearch(killNewlines(msg))

				// display message to the sender
				var/Time = person.getTime()
				if(person.Colors) person<<output("<b>\[</b>[Time]<b>\] <font color='#[Name_Color]'>[src.name]</font></b>: <font color='#[Text_Color]'>[msg]</font>","IM_[src.ckey].IM_output")
				else person<<output("<b>\[</b>[Time]<b>\] [src.name]</b>: [msg]","IM_[src.ckey].IM_output")

				// display message to the receiver
				Time = src.getTime()
				if(src.Colors) src<<output("<b>\[</b>[Time]<b>\] <font color='#[Name_Color]'>[src.name]</font></b>: <font color='#[Text_Color]'>[msg]</font>","IM_[person.ckey].IM_output")
				else src<<output("<b>\[</b>[Time]<b>\] [name]</b>: [msg]","IM_[person.ckey].IM_output")

				OutputUserLog(person, src, msg)

				//update images in case they've changed
				winset(person, "IM_[src.ckey].IM_image1", "image=\"[person.User_Image]\"; image-mode=stretch")
				winset(person, "IM_[src.ckey].IM_image2", "image=\"[src.User_Image]\"; image-mode=stretch")
				winset(src, "IM_[person.ckey].IM_displayname1", "text=\"[src.key]\"")
				winset(src, "IM_[person.ckey].IM_displayname2", "text=\"[person.key]\"")

				//winset(person,"Main","flash=-1")